package pl.edu.utp.kursy.database.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import pl.edu.utp.kursy.database.Course;

public class CourseDao extends BaseDao<Course>{
    
    public CourseDao(SessionFactory sessionFactory){
        super(sessionFactory);
    }

    @Override
    public Course getById(int id) {
        Session session = sessionFactory.openSession();
        
        Criteria c = session.createCriteria(Course.class);
        c.add(Restrictions.eq("id", id));    		    		
        Course course = (Course)c.uniqueResult();
        
        session.close(); 
        return course;
    }

    @Override
    public List<Course> getAll() {
        ArrayList<Course> courses;
        
        Session session = sessionFactory.openSession();
        Criteria c = session.createCriteria(Course.class);
        c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        courses = (ArrayList<Course>)c.list();
        session.close();
        
        return courses; 
    }
    
}
